; Template
;(deftemplate emergency ( slot location ))

; Outputs
(deffunction emergency-print () (printout t "There is an emergency" crlf))

;(watch all)

; Rules
; west rules
(defrule p-light-1 (PWEW) (LWNS) => (assert (w-pause-continue)))
(defrule p-light-2 (PWNS) (LWEW) => (assert (w-pause-continue)))

(defrule wns-light-rule (wn-light) (ws-light) => (assert (wns-light)))
(defrule wew-light-rule (we-light) (ww-light) => (assert (wew-light)))
(defrule wns-heavy-rule (wn-heavy) (ws-heavy) => (assert (wns-heavy)))
(defrule wew-heavy-rule (we-heavy) (ww-heavy) => (assert (wew-heavy)))

(defrule w-rule-0-1 (wns-light) (wew-light) (no-w-emergency) => (assert (west-traffic-light normal)))

(defrule w-rule-1-3 (wns-heavy) (we-heavy) (ww-light) (no-w-emergency) => (assert (west-traffic-light normal)))
(defrule w-rule-1-4 (wns-heavy) (ww-heavy) (we-light) (no-w-emergency) => (assert (west-traffic-light normal)))

(defrule w-rule-2-1 (wew-heavy) (LWEW) (no-w-emergency) => (assert (west-traffic-light-extend)))
(defrule w-rule-2-2 (wew-heavy) (LWNS) (wn-light) (no-w-emergency) => (assert (west-traffic-light change)))
(defrule w-rule-2-3 (wew-heavy) (LWNS) (ws-light) (no-w-emergency) => (assert (west-traffic-light change)))

(defrule w-rule-4-1 (we-heavy) (LWNS) (wn-light) (no-w-emergency) => (assert (west-traffic-light shorten)))
(defrule w-rule-4-2 (we-heavy) (LWNS) (ws-light) (no-w-emergency) => (assert (west-traffic-light shorten)))
(defrule w-rule-4-3 (ww-heavy) (LWNS) (wn-light) (no-w-emergency) => (assert (west-traffic-light shorten)))
(defrule w-rule-4-4 (ww-heavy) (LWNS) (ws-light) (no-w-emergency) => (assert (west-traffic-light-shorten)))

(defrule w-rule-5-1 (we-heavy) (LWEW) (wn-light) (no-w-emergency) => (assert (west-traffic-light extend)))
(defrule w-rule-5-2 (we-heavy) (LWEW) (ws-light) (no-w-emergency) => (assert (west-traffic-light extend)))
(defrule w-rule-5-3 (ww-heavy) (LWEW) (wn-light) (no-w-emergency) => (assert (west-traffic-light extend)))
(defrule w-rule-5-4 (ww-heavy) (LWEW) (ws-light) (no-w-emergency) => (assert (west-traffic-light extend)))

(defrule w-rule-6-1 (wew-light) (LWNS) (wn-heavy) (no-w-emergency) => (assert (west-traffic-light extend)))
(defrule w-rule-6-2 (wew-light) (LWNS) (ws-heavy) (no-w-emergency) => (assert (west-traffic-light extend)))
(defrule w-rule-6-3 (wew-light) (LWEW) (wn-heavy) (no-w-emergency) => (assert (west-traffic-light shorten)))
(defrule w-rule-6-4 (wew-light) (LWEW) (ws-heavy) (no-w-emergency) => (assert (west-traffic-light shorten)))

(defrule w-emergency-1-2 (emergency WW) (LWEW) => (assert (west-traffic-light shorten)))
(defrule w-emergency-1-3 (emergency M) (LWEW) => (assert (west-traffic-light shorten)))
(defrule w-emergency-1-4 (emergency WN) (LWNS) => (assert (west-traffic-light shorten)))
(defrule w-emergency-1-5 (emergency WS) (LWNS) => (assert (west-traffic-light shorten)))

(defrule w-emergency-2-2 (emergency WW) (LWNS) => (assert (west-traffic-light extend)))
(defrule w-emergency-2-3 (emergency M) (LWNS) => (assert (west-traffic-light extend)))
(defrule w-emergency-2-4 (emergency WN) (LWEW) => (assert (west-traffic-light extend)))
(defrule w-emergency-2-5 (emergency WS) (LWEW) => (assert (west-traffic-light extend)))

(defrule w-emergency-3 (emergency W) => (assert (west-traffic-light terminate)))

; east rules

(defrule p-light-3 (PEEW) (LENS) => (assert (e-pause-continue)))
(defrule p-light-4 (PENS) (LEEW) => (assert (e-pause-continue)))

(defrule ens-light-rule (en-light) (es-light) => (assert (ens-light)))
(defrule eew-light-rule (ee-light) (ew-light) => (assert (eew-light)))
(defrule ens-heavy-rule (en-heavy) (es-heavy) => (assert (ens-heavy)))
(defrule eew-heavy-rule (ee-heavy) (ew-heavy) => (assert (eew-heavy)))

(defrule e-rule-0-1 (ens-light) (eew-light) (no-e-emergency) => (assert (east-traffic-light normal)))

(defrule e-rule-1-3 (ens-heavy) (ee-heavy) (ew-light) (no-e-emergency) => (assert (east-traffic-light normal)))
(defrule e-rule-1-4 (ens-heavy) (ew-heavy) (ee-light) (no-e-emergency) => (assert (east-traffic-light normal)))

(defrule e-rule-2-1 (eew-heavy) (LEEW) (no-e-emergency) => (assert (east-traffic-light-extend)))
(defrule e-rule-2-2 (eew-heavy) (LENS) (en-light) (no-e-emergency) => (assert (east-traffic-light change)))
(defrule e-rule-2-3 (eew-heavy) (LENS) (es-light) (no-e-emergency) => (assert (east-traffic-light change)))

(defrule e-rule-4-1 (ee-heavy) (LENS) (en-light) (no-e-emergency) => (assert (east-traffic-light shorten)))
(defrule e-rule-4-2 (ee-heavy) (LENS) (es-light) (no-e-emergency) => (assert (east-traffic-light shorten)))
(defrule e-rule-4-3 (ew-heavy) (LENS) (en-light) (no-e-emergency) => (assert (east-traffic-light shorten)))
(defrule e-rule-4-4 (ew-heavy) (LENS) (es-light) (no-e-emergency) => (assert (east-traffic-light shorten)))

(defrule e-rule-5-1 (ee-heavy) (LEEW) (en-light) (no-e-emergency) => (assert (east-traffic-light extend)))
(defrule e-rule-5-2 (ee-heavy) (LEEW) (es-light) (no-e-emergency) => (assert (east-traffic-light extend)))
(defrule e-rule-5-3 (ew-heavy) (LEEW) (en-light) (no-e-emergency) => (assert (east-traffic-light extend)))
(defrule e-rule-5-4 (ew-heavy) (LEEW) (es-light) (no-e-emergency) => (assert (east-traffic-light extend)))

(defrule e-rule-6-1 (eew-light) (LENS) (en-heavy) (no-e-emergency) => (assert (east-traffic-light extend)))
(defrule e-rule-6-2 (eew-light) (LENS) (es-heavy) (no-e-emergency) => (assert (east-traffic-light extend)))
(defrule e-rule-6-3 (eew-light) (LEEW) (en-heavy) (no-e-emergency) => (assert (east-traffic-light shorten)))
(defrule e-rule-6-4 (eew-light) (LEEW) (es-heavy) (no-e-emergency) => (assert (east-traffic-light shorten)))

(defrule e-emergency-1-2 (emergency EE) (LEEW) => (assert (east-traffic-light shorten)))
(defrule e-emergency-1-3 (emergency M) (LEEW) => (assert (east-traffic-light shorten)))
(defrule e-emergency-1-4 (emergency EN) (LENS) => (assert (east-traffic-light shorten)))
(defrule e-emergency-1-5 (emergency ES) (LENS) => (assert (east-traffic-light shorten)))

(defrule e-emergency-2-2 (emergency EE) (LENS) => (assert (east-traffic-light extend)) (assert (e-emergency)))
(defrule e-emergency-2-3 (emergency M) (LENS) => (assert (east-traffic-light extend)) (assert (e-emergency)))
(defrule e-emergency-2-4 (emergency EN) (LEEW) => (assert (east-traffic-light extend)) (assert (e-emergency)))
(defrule e-emergency-2-5 (emergency ES) (LEEW) => (assert (east-traffic-light extend)) (assert (e-emergency)))

(defrule e-emergency-3 (emergency E) => (assert (east-traffic-light terminate)))

; combined rules
(defrule rule-1 (LWNS) (LEEW) (west-traffic-light normal) (east-traffic-light normal) => (assert (west-traffic-light change)))
(defrule rule-2 (LWEW) (LENS) (west-traffic-light normal) (east-traffic-light normal) => (assert (east-traffic-light change)))
(defrule rule-3 (west-traffic-light normal) (LWNS) (east-traffic-light change) (LENS) => (assert (west-traffic-light shorten)))
(defrule rule-4 (west-traffic-light change) (LWNS) (east-traffic-light normal) (LENS) => (assert (east-traffic-light shorten)))
(defrule rule-5 (west-traffic-light terminate) (LENS) (east-traffic-light change) => (assert(east-traffic-light extend)))
(defrule rule-6 (west-traffic-light terminate) (LEEW) (east-traffic-light extend) => (assert(east-traffic-light shorten)))
(defrule rule-7 (east-traffic-light terminate) (LWNS) (west-traffic-light change) => (assert(west-traffic-light extend)))
(defrule rule-8 (east-traffic-light terminate) (LWEW) (west-traffic-light extend) => (assert(west-traffic-light shorten)))
(defrule rule-9 (w-emergency) (LWEW) (LEEW) (east-traffic-light extend) => (assert(east-traffic-light normal)))
(defrule rule-10 (w-emergency) (LWEW) (LENS) (east-traffic-light shorten) => (assert(east-traffic-light normal)))
(defrule rule-11 (e-emergency) (LWEW) (LEEW) (west-traffic-light extend) => (assert(west-traffic-light normal)))
(defrule rule-12 (e-emergency) (LWNS) (LEEW) (west-traffic-light shorten) => (assert(west-traffic-light normal)))
